#!/usr/bin/env python
# -*- coding: utf-8 -*-


__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# Python
import sys
sys.path.append(".")
sys.path.append("src")

import ConfigParser
import traceback
import codecs

# Mikan
from mikan.orm import new_mikan_orm

def mikan_metadata():
	config = ConfigParser.SafeConfigParser()
	try:
		config.readfp(codecs.open("etc/mikan.ini", "r", "utf8"))
	except:
		print(traceback.format_exc())

	ini = ConfigParser.SafeConfigParser()
	try:
		ini.readfp(codecs.open("./etc/orm.ini", "r", "utf8"))
	except:
		print(traceback.format_exc())

	try:
		t = config.get("orm", "type")
		orm = new_mikan_orm(None, t)
		orm.setup(config, "orm")
	except Exception, e:
		print(traceback.format_exc())

	for s in ini.sections():
		for o in ini.options(s):
			try:
				name = ini.get(s, o)
				# モジュールインポート
				module = __import__(s, fromlist=[s])
				clazz = getattr(module, name)

				orm.mapper_entity(clazz())
			except:
				print(traceback.format_exc())

	print orm.metadata.sorted_tables
	return orm.metadata

# ------------------------------------------------------------
## メイン処理
if __name__ == '__main__':
	pass
#	config = ConfigParser.SafeConfigParser()
#	try:
#		config.readfp(codecs.open("etc/mikan.ini", "r", "utf8"))
#	except:
#		print(traceback.format_exc())
#
#	ini = ConfigParser.SafeConfigParser()
#	try:
#		ini.readfp(codecs.open("./etc/orm.ini", "r", "utf8"))
#	except:
#		print(traceback.format_exc())
#
#	try:
#		t = config.get("orm", "type")
#		orm = new_mikan_orm(None, t)
#		orm.setup(config, "orm")
#	except Exception, e:
#		print(traceback.format_exc())

#	for s in ini.sections():
#		for o in ini.options(s):
#			try:
#				name = ini.get(s, o)
#				# モジュールインポート
#				module = __import__(s, fromlist=[s])
#				clazz = getattr(module, name)
#
#				orm.mapper_entity(clazz())
#			except:
#				print(traceback.format_exc())
#
#	orm.metadata.create_all()
